package cn.mastor.system.service.impl;

import cn.mastor.basic.service.impl.BaseServiceImpl;
import cn.mastor.system.domain.Menu;
import cn.mastor.system.domain.Role;
import cn.mastor.system.mapper.RoleMapper;
import cn.mastor.system.service.IRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Long> findByroId(Long id) {
        return roleMapper.findByroId(id);
    }

    @Override
    public List<Long> findBypeId(Long id) {
        return roleMapper.findBypeId(id);
    }

    @Override
    public List<Menu> findmenuAll(Long id) {
        return roleMapper.findmenuAll(id);
    }

    @Override
    public void deletepermission(Long id) {
        roleMapper.deletepermission(id);
    }

    @Override
    public void deletemenu(Long id) {
        roleMapper.deletemenu(id);
    }

    @Override
    public void update(Role role) {
        roleMapper.deletemenu(role.getId());
        roleMapper.deletepermission(role.getId());
        roleMapper.updatemenus(role.getId(),role.getMenus());
        roleMapper.updatepermissions(role.getId(),role.getPermissions());
//        roleMapper.delete(role.getId());
//        roleMapper.add(role);
//        role.setId(role.getId()-1);
//        roleMapper.update(role);
//        return AjaxResult.me().success();
    }
    @Override
    public void add(Role role) {
        roleMapper.add(role);
//        roleMapper.deletemenu(role.getId());
//        roleMapper.deletepermission(role.getId());
        roleMapper.updatemenus(role.getId(),role.getMenus());
        roleMapper.updatepermissions(role.getId(),role.getPermissions());
//        roleMapper.delete(role.getId());

//        role.setId(role.getId()-1);
//        roleMapper.update(role);
//        return AjaxResult.me().success();
    }

    @Override
    public void delete(Long id) {
        roleMapper.deletepermission(id);
        roleMapper.deletemenu(id);
        roleMapper.delete(id);
    }
}
